package cn.gdcp.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private Button startService;
private Button stopService;
private Button bundlerService;
private Button disbundlerService;
private ServiceConnection serviceConnection;
private Mybinder mybinder;
private Button getTime;
private EditText editEnglish;
private Button reverseEnglish;
private TextView showEnglish;
private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBundler();
        startServiceEvent();
        stopServiceEvent();
        bundlerServiceEvent();
        getTimeEvent();
        reverseEnglishEvent();
    }
    private void reverseEnglishEvent() {
              reverseEnglish.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      String str=editEnglish.getText().toString();
                      String reverseResult= mybinder.reverseEnglish(str);
                      showEnglish.setText(reverseResult);
                  }
              });
    }

    private void getTimeEvent() {
        getTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mybinder.showTime(MainActivity.this);
            }
        });
    }
    private void bundlerServiceEvent() {
               bundlerService.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       Intent intent=new Intent();
                       intent.setClass(MainActivity.this,MyService.class);
                       bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                   }
               });
    }
    private void stopServiceEvent() {
             stopService.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                        Intent intent =new Intent();
                        intent.setClass(MainActivity.this,MyService.class);
                        stopService(intent);
                 }
             });
    }

    private void startServiceEvent() {
        startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });

    }

    private void initBundler() {
        startService=findViewById(R.id.start_service);
        stopService=findViewById(R.id.stop_service);
        bundlerService=findViewById(R.id.bundler_service);
        disbundlerService=findViewById(R.id.disbundler_service);
        /*   创建 serviceConnection */
        serviceConnection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                     mybinder= (Mybinder) iBinder;
                Log.e(TAG, "onServiceConnected: " );
            }
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e(TAG, "onServiceDisconnected: " );
            }
        };
        /*  在此结束创建connection 对象*/

        getTime=findViewById(R.id.get_time);
        editEnglish=findViewById(R.id.edit_English);
        reverseEnglish=findViewById(R.id.reverse_English);
        showEnglish=findViewById(R.id.show_English);
    }
}
